let myTools = require('../utils/mytools');

class SpriteFrameCache{

    constructor(){
        this._cache = {};
    }

    add(spriteFrame){
        if(!spriteFrame){
            throw new Error(`spriteFrame is null!`);
        }
        if(this._cache[spriteFrame.id]){
            console.warn(`spriteFrame:${spriteFrame.id} already add to cache`);
            return;
        }
        this._cache[spriteFrame.id] = spriteFrame;
    }


    /**
     *
     * @param idOrFrame {number | SpriteFrame}
     */
    remove(idOrFrame){
        if(myTools.Types.isNumber(idOrFrame)){
            if(this._cache[idOrFrame]){
                delete this._cache[idOrFrame];
            }
        }else{
            let id = idOrFrame.id;
            if(this._cache[id]){
                delete this._cache[id];
            }
        }
    }

    /**
     *
     * @param id {number}
     * @return {SpriteFrame | null}
     */
    get(id){
        return this._cache[id];
    }

    clean(){
        this._cache = {};
    }
}

module.exports = SpriteFrameCache;
